package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.ConfirmAnOrderResponse
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ItemConfirmOrderShopBinding

/**
 * @author: hxw
 * @date  : 2023/2/2
 * @desc  :
 */
class ConfirmOrderAdapter(data: ArrayList<ConfirmAnOrderResponse.StoreInfoList>) :
    BaseQuickAdapter<ConfirmAnOrderResponse.StoreInfoList, DataBindBaseViewHolder>(R.layout.item_confirm_order_shop, data) {

    private var onBtnReturnClickListener: OnBtnReturnClickListener? = null

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: ConfirmAnOrderResponse.StoreInfoList) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemConfirmOrderShopBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        holder.itemView.apply {
            binding.rvItemGoodsOrderDetail.adapter = null
            if (item.isEmpty() || item.list.size == 0) {
                holder.setGone(R.id.rvItemGoodsOrderDetail, true)//隐藏
                return
            }
            holder.setGone(R.id.rvItemGoodsOrderDetail, false)//显示
            val mAdapter = OrderDetailGoodsAdapter(item.list)
            binding.rvItemGoodsOrderDetail.apply {
                layoutManager = LinearLayoutManager(context)
                binding.rvItemGoodsOrderDetail.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 10f)))
                adapter = mAdapter
                mAdapter.notifyDataSetChanged()
            }
            mAdapter.setOnReturnGoodsClickListener(object : OrderDetailGoodsAdapter.OnBtnReturnClickListener {
                override fun onItemClick(position: Int) {
                    onBtnReturnClickListener?.onBtnReturnClick(
                        position,
                        mAdapter.data[position].goodId,
                        mAdapter.data[position].id,
                        mAdapter.data[position].childOrderStatus,
                        item.shopId,
                        item.shopOrderId
                    )
                }
            })
        }
    }

    fun setOnReturnGoodsClickListener(listener: OnBtnReturnClickListener?) {
        onBtnReturnClickListener = listener
    }

    interface OnBtnReturnClickListener {
        fun onBtnReturnClick(position: Int, goodsId: Int, orderId: Int, orderStatus: Int, shopId: Int, shopOrderId: Int)
    }
}